package richbar.com.richbar.view.pop;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.DisplayUtil;
import com.common.util.StringUtil;

import richbar.com.richbar.R;

/**
 * Created by qyang on 2018-8-13.
 */

public class FilterPop {
    private BaseActivity activity;
    public FilterPop(BaseActivity activity) {
        this.activity = activity;
    }

    public void showFilter( View view,  Callback callback){
        View popupView = LayoutInflater.from(activity).inflate(R.layout.pop_filter, null);
        PopupWindow popupWindow = new PopupWindow(
                DisplayUtil.getWidth(activity) / 64 * 57, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(popupView);
        setView(popupView,popupWindow,callback);
        //点击空白区域PopupWindow消失，这里必须先设置setBackgroundDrawable，否则点击无反应
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setClippingEnabled(true);
        popupWindow.setAnimationStyle(R.style.AnimPop_r_l_r);
        popupWindow.setFocusable(true);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (activity != null) {
                    setBackgroundAlpha(activity, 1.0f);
                }
            }
        });
        popupWindow.showAtLocation(view, Gravity.RIGHT | Gravity.TOP,0,0);
        setBackgroundAlpha(activity,0.5f);//设置屏幕透明度
    }

    private void setView(View view, final PopupWindow popupWindow, final Callback callback) {
        final TextView price_min = view.findViewById(R.id.price_min);
        final TextView price_max = view.findViewById(R.id.price_max);
        view.findViewById(R.id.reset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                price_min.setText("");
                price_max.setText("");
            }
        });
        view.findViewById(R.id.besure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                callback.onFilter(StringUtil.getStr(price_min), StringUtil.getStr(price_max));
            }
        });
    }

    private void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1.0f) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }



    public interface Callback{
        void onFilter(String minPrice,String maxPrice);
    }

}
